export const getRedirectUrl = () => {
  return `${
    import.meta.env.VITE_SSO_LOGIN_URL
  }?redirect_uri=${encodeURIComponent(window.location.href)}`;
};

export const throttle = () => {};
// 防抖：在满足某种条件后才执行我传给你的函数
export const debounce = (fn) => {
  // 闭包导致timer被缓存，不会被垃圾回收
  let timer;
  return function (params) {
    if (timer) clearTimeout(timer);
    timer = setTimeout(() => {
      fn(params);
    }, 500);
  };
};

export const isMobile = () => {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
    navigator.userAgent
  );
};

export const flushObject = (obj) => {
  const origObj = { ...obj };
  // 清空对象中的无效数据，包括空数组
  for (let key in obj) {
    const isValid =
      typeof obj[key] === "string" ||
      typeof obj[key] === "number" ||
      (Array.isArray(obj[key]) && obj[key].length);
    if (!isValid) {
      delete origObj[key];
    }
  }

  return origObj;
};
